package ext.tianma.part.finishedProduct.builder;


import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.csm.navigation.ClassificationNode;
import wt.csm.navigation.litenavigation.ClassificationNodeDefaultView;
import wt.csm.navigation.service.ClassificationObjectsFactory;
import wt.fc.WTObject;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.ui.resources.ComponentMode;
import com.ptc.core.ui.resources.ComponentType;
import com.ptc.jca.mvc.components.AbstractAttributesComponentBuilder;
import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AttributeConfig;
import com.ptc.mvc.components.AttributePanelConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.CustomizableViewConfig;
import com.ptc.mvc.components.GroupConfig;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.beans.NmHelperBean;

import ext.tianma.part.finishedProduct.util.CSMUtil;
import ext.tianma.part.finishedProduct.util.LoadAttrsConfig;
import ext.tianma.part.util.IbaAttributeUtil;
import ext.tianma.util.PropertiesUtil;

@ComponentBuilder("finishedGood.builder.editFinishedGoodAttrsBuilder")
public class EditFinishedGoodAttrsBuilder extends AbstractAttributesComponentBuilder {
	private static Logger log = LogR.getLogger(EditFinishedGoodAttrsBuilder.class.getName());
	@Override
	protected CustomizableViewConfig buildAttributesComponentConfig(
			ComponentParams paramComponentParams) throws WTException {
		log.debug("EditFinishedGoodAttrsBuilder::start");
		NmHelperBean nmhelperbean = ((JcaComponentParams)paramComponentParams).getHelperBean();
		String roleName = String.valueOf(nmhelperbean.getRequest().getParameter("roleName"));
		NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();
        NmOid nmoid = nmcommandbean.getActionOid();
        if(nmoid == null){
            nmoid = nmcommandbean.getPrimaryOid();
        }
        
        ComponentConfigFactory factory = getComponentConfigFactory();
        AttributePanelConfig panelConfig = factory.newAttributePanelConfig();//ComponentId.ATTRIBUTE_PANEL_ID
        
        WTObject contextObj = (WTObject) nmoid.getWtRef().getObject();
    	ClassificationNode node = CSMUtil.getClassNodeByPart((WTPart) contextObj);
    	if(null == node){
    		throw new WTException("没有获取到部件分类");
    	}
    	ClassificationNodeDefaultView cndv=ClassificationObjectsFactory.newClassificationNodeDefaultView(node);
    	String fullPath = CSMUtil.getClassificationFullPath(cndv);
		
		
		
		panelConfig.setComponentMode(ComponentMode.EDIT);
		panelConfig.setComponentType(ComponentType.WIZARD_ATTRIBUTES_TABLE);
		
		GroupConfig group = factory.newGroupConfig();
		group.setId("partIBAs.edit.id");
		group.setLabel("成品类型属性");
		group.setComponentMode(ComponentMode.EDIT);
		
		GroupConfig classAttrGroup = factory.newGroupConfig();
		classAttrGroup.setId("partClassIBAs.edit.id");
		classAttrGroup.setLabel("分类属性");
		classAttrGroup.setComponentMode(ComponentMode.EDIT);
		
		//获取属性配置信息;
		String editAttrs = "";
		String viewAttrs = "";
		try {
			String finishedGoodDimension = IbaAttributeUtil.getAttributeValue(contextObj, "finishedGoodDimension");
			log.debug("EditFinishedGoodAttrsBuilder:partNumber==>>"+((WTPart) contextObj).getNumber() 
					+ ";roleName==>>"+roleName+ ";finishedGoodDimension==>>"+finishedGoodDimension);
			if(StringUtils.isEmpty(finishedGoodDimension)){
				throw new WTException("请联系管理员，'产品维度'属性没有维护!");
			}
			editAttrs = PropertiesUtil.getValueByKey(fullPath + "." + finishedGoodDimension +"." +roleName + ".EDIT", LoadAttrsConfig.configFilePath);
			log.debug("editAttrs==>>"+editAttrs);
			viewAttrs = PropertiesUtil.getValueByKey(fullPath + "."+ finishedGoodDimension +"."  + roleName + ".VIEW", LoadAttrsConfig.configFilePath);
			log.debug("viewAttrs==>>"+viewAttrs);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//动态生成编辑组件
		if(StringUtils.isNotEmpty(editAttrs)){
			//获取分类节点下所有属性   Map<内部名称,显示名称>
		    Map<String, String> classNodeAllIBAs = CSMUtil.getClassNodeAllIBAs(node);
		    String[] attrArry = editAttrs.split(";");
		    boolean isHasClassAttrs = false;
		    boolean isHasPdmAttrs = false;
			for(String attrInternalName : attrArry){
				if(classNodeAllIBAs.containsKey(attrInternalName)){
					//分类属性
					classAttrGroup.addComponent(newAttribute(attrInternalName,attrInternalName, factory,true,"2"));
					isHasClassAttrs = true;
				}else{
					//PDM属性
					group.addComponent(newAttribute(attrInternalName,attrInternalName, factory,true,"1"));
					isHasPdmAttrs = true;
				}
			}
			if(isHasPdmAttrs){
				panelConfig.addComponent(group);
			}
			
			if(isHasClassAttrs){
				panelConfig.addComponent(classAttrGroup);
			}
		}
		
		//动态生成查看组件
		if(StringUtils.isNotEmpty(viewAttrs)){
			String[] viewAttrArry = viewAttrs.split(";");
			GroupConfig viewGroup = factory.newGroupConfig();
			viewGroup.setId("partIBAs.view.id");
			viewGroup.setLabel("查看属性");
			//viewGroup.setComponentMode(ComponentMode.VIEW);
			for(String attrInternalName : viewAttrArry){
				viewGroup.addComponent(newAttribute(attrInternalName,attrInternalName, factory,false,""));
			}
			panelConfig.addComponent(viewGroup);
		}
		return panelConfig;
	}
	
	private static AttributeConfig newAttribute(String id, String label ,ComponentConfigFactory factory,boolean isEdit,String type){
		AttributeConfig attribute = factory.newAttributeConfig();
		attribute.setId(id);
		//attribute.setLabel(label);
		if(isEdit){
			if("1".equals(type)){
				attribute.setDataUtilityId("partIBAsDataUtility.id");
			}else if("2".equals(type)){
				attribute.setDataUtilityId("classIBAsDataUtility.id");
			}
		}
		return attribute;
	}
}
